package com.csu.cphone.service.impl;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.fileupload.FileItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.csu.common.constants.CommonParams;
import com.csu.common.utils.FileUtil;
import com.csu.common.utils.JSONUtil;
import com.csu.common.utils.RedisUtil;
import com.csu.cphone.constants.RedisKey;
import com.csu.cphone.dao.PhoneBrandMapper;
import com.csu.cphone.dao.PhoneMapper;
import com.csu.cphone.pojo.PhoneBrandPojo;
import com.csu.cphone.pojo.PhonePojo;
import com.csu.cphone.service.PhoneService;
import com.csu.cphone.vo.HotPhone;

/**
 * @项目名称：cphone
 * @类名称：PhoneServiceImpl
 * @类描述：
 * @创建人：Tangc
 * @创建时间：2017年9月14日 下午2:58:48
 * @company:
 */
@Service
public class PhoneServiceImpl implements PhoneService
{

    @Autowired
    private PhoneBrandMapper phoneBrandMapper;

    @Autowired
    private PhoneMapper phoneMapper;

    /**
     * @see com.csu.cphone.service.PhoneService#getHotRecycleList()
     */
    @Override
    public List<HotPhone> getHotRecycleList()
    {
        List<HotPhone> result = new ArrayList<HotPhone>();
        List<String> list = RedisUtil.getRedisDb().lrange(RedisKey.Hot_Recycle_Phone, 0, -1);
        for (String json : list)
        {
            HotPhone phone = JSONUtil.toObject(json, HotPhone.class);
            result.add(phone);
        }
        return result;
    }

    /**
     * @see com.csu.cphone.service.PhoneService#wirteFile(org.springframework.web.multipart.commons.CommonsMultipartFile)
     */
    @Override
    public String wirteFile(CommonsMultipartFile productFile)
    {
        String requestUrl = null;
        if (productFile.getSize() == 0 || productFile == null)
        {
            requestUrl = "";
        }
        else
        {
            FileItem fileItem = productFile.getFileItem();
            String fileName = fileItem.getName();

            try
            {
                fileName = new String(fileName.getBytes(), "UTF-8");
            }
            catch (UnsupportedEncodingException e1)
            {
                e1.printStackTrace();
            }

            fileName = System.currentTimeMillis() + "." + fileName;
            requestUrl = CommonParams.REQUEST_URL + "/" + fileName;
            try
            {
                FileUtil.writeFile(productFile, fileName);
            }
            catch (FileNotFoundException e)
            {
                e.printStackTrace();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }
        return requestUrl;
    }

    /**
     * @see com.csu.cphone.service.PhoneService#addBrand(java.lang.String,
     *      java.lang.String)
     */
    @Override
    public PhoneBrandPojo addBrand(String brandName, String url)
    {
        PhoneBrandPojo phoneBrand = new PhoneBrandPojo();
        phoneBrand.setPicUrl(url);
        phoneBrand.setBrandName(brandName);
        phoneBrandMapper.insert(phoneBrand);
        return phoneBrand;
    }

    /**
     * @see com.csu.cphone.service.PhoneService#addPhone(int, java.lang.String,
     *      java.lang.String, int, int)
     */
    @Override
    public PhonePojo addPhone(int brandId, String name, String url, int isUseRecycle, int isUsemaintain)
    {
        PhonePojo phone = new PhonePojo();
        phone.setBrandId(brandId);
        phone.setMaintainStatus(isUsemaintain);
        phone.setName(name);
        phone.setPicUrl(url);
        phone.setRecycleStatus(isUseRecycle);
        phoneMapper.insert(phone);
        return null;
    }

    /**
     * @see com.csu.cphone.service.PhoneService#addToHotRecycleList(com.csu.cphone.vo.HotPhone)
     */
    @Override
    public int addToHotRecycleList(HotPhone hotPhone)
    {
        String json = JSONUtil.toJSONString(hotPhone);
        Long result = RedisUtil.getRedisDb().lpush(RedisKey.Hot_Recycle_Phone, json);
        return result.intValue();
    }

    /**
     * @see com.csu.cphone.service.PhoneService#getBrandList()
     */
    @Override
    public List<PhoneBrandPojo> getBrandList()
    {
        return phoneBrandMapper.selectBrandList();
    }

    /**
     * @see com.csu.cphone.service.PhoneService#getRecyclePhoneList(int)
     */
    @Override
    public List<PhonePojo> getRecyclePhoneList(int brandId)
    {
        return phoneMapper.selectRecyclePhoneList(brandId);
    }

}
